﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.StarterSite.Controllers;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Models.ViewData;
using MvcEngine.Blog.Filters;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Components.Extensions;
using System.Web.Mvc;
using MvcEngine.Blog.Models;
using MvcEngine.Mvc.Controllers;

namespace MvcEngine.Blog.StarterSite.Controllers
{
    public class BlogTagController : SubController
    {
        private IBlogTagRepository tagRepository = null;

        public BlogTagController(IBlogTagRepository tagRepository)
        {
            this.tagRepository = tagRepository;
        }

        public MvcEngineModel Index(Guid blogId)
        {
            List<KeyValuePair<BlogTag, int>> tags = tagRepository.GetTagsWithPostCount(blogId).ToList();
            if (tags.Count == 0)
            {
                return new MvcEngineModel() { ViewName = "Empty" };
            }

            int max = 0;
            tags.ForEach(a => { if (((int)a.Value) > max) max = ((int)a.Value); });

            IList<KeyValuePair<BlogTag, string>> result = new List<KeyValuePair<BlogTag, string>>();

            foreach (var tag in tags)
            {
                string ccsClass = string.Empty;
                double weight = ((double)((int)tag.Value) / max) * 100;
                if (weight >= 99)
                    ccsClass = "biggest";
                else if (weight >= 70)
                    ccsClass = "big";
                else if (weight >= 40)
                    ccsClass = "medium";
                else if (weight >= 20)
                    ccsClass = "small";
                else if (weight >= 0)
                    ccsClass = "smallest";
                result.Add(new KeyValuePair<BlogTag, string>(tag.Key, ccsClass));
            }

            return new MvcEngineModelList<KeyValuePair<BlogTag, string>>() { List = result, ViewName = "Index" };
        }
    }
}
